package com.parktech.sun.web.controller;

import com.parktech.sun.domain.model.email.EmailSubscription;
import com.parktech.sun.domain.service.email.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * User: Think
 * Date: 11-1-28
 * Time: 上午3:24
 */
@Controller
public class EmailSubscriptionController {

    private EmailService emailService;

    @Autowired
    public EmailSubscriptionController(EmailService emailService) {
        this.emailService = emailService;
    }

    @RequestMapping("/subscription")
    public
    @ResponseBody
    String apply(@RequestParam("email") String email) {

        EmailSubscription emailSubscription = emailService.getByEmail(email);

        if (emailSubscription == null) {
            emailSubscription = new EmailSubscription();
            emailSubscription.setEmail(email);

            emailService.saveOrUpdateBean(emailSubscription);
        }
        return "success";
    }
}
